package database; 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


/** Database connection class & utilities **/

abstract class DBBase {

	public static  String sDriver = ""; 
	public static  String sUrl = null;
	public static  int iTimeout = 30;
	public static  Connection conn = null;
	public static  Statement statement = null;


	/* Stub constructor for quick instantiation o/t fly for using some of the ancillary stuff */
	public  DBBase()
	{}

	/* quick and dirty constructor to test the database passing the DriverManager name and the fully loaded url to handle */
	/* NB this will typically be available if you make this class concrete and not abstract */
	public DBBase(String sDriverToLoad, String sUrlToLoad) throws Exception
	{
		init(sDriverToLoad, sUrlToLoad);
	}

	public static void init(String sDriverVar, String sUrlVar) throws Exception
	{
		setDriver(sDriverVar);
		setUrl(sUrlVar);
		setConnection();
		setStatement();
	}

	private static void setDriver(String sDriverVar)
	{
		sDriver = sDriverVar;
	}

	private static void setUrl(String sUrlVar)
	{
		sUrl = sUrlVar;
	}

	public static  void setConnection() throws Exception {
		Class.forName(sDriver);
		conn = DriverManager.getConnection(sUrl);
	}


	public  Connection getConnection() {
		return conn;
	}

	public static  void setStatement() throws Exception {
		if (conn == null) {
			setConnection();
		}
		statement = conn.createStatement();
		statement.setQueryTimeout(iTimeout);  // set timeout to 30 sec.
	}

	public  Statement getStatement() {
		return statement;
	}

	public  void executeStmt(String instruction) throws SQLException {
		statement.executeUpdate(instruction);
	}

	// processes an array of instructions e.g. a set of SQL command strings passed from a file
	//NB you should ensure you either handle empty lines in files by either removing them or parsing them out 
	// since they will generate spurious SQLExceptions when they are encountered during the iteration....
	public void executeStmt(String[] instructionSet) throws SQLException {
		for (int i = 0; i < instructionSet.length; i++) {
			executeStmt(instructionSet[i]);
		}
	}

	public ResultSet executeQry(String instruction) throws SQLException {
		return statement.executeQuery(instruction);
	} 

	public static void closeConnection() {
		try { conn.close(); } catch (Exception ignore) {}
	}

}